package com.weipu.common.facade.model;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.weipu.common.constants.ServerConstant;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * @author TonyYang Coupon的数据模型
 */
public class CouponModel implements Parcelable{


	// 活动编号
	private String CouponCode;

	//活动名称
	private String Name;

	// 有效期开始时间
	private String BeginDate;

	// 有效期结束时间
	private String EndDate;

	//活动条件描述
	private String ConditionDes;

	//活动内容描述
	private String ActionDes;

	// 活动描述
	private String Description;
	
	// Coupon类型，活动状态
	private String Status;
	
	// 订单号
	private String OrderNumber;
	
	// 图片信息
	private String ImgUrl;

	private int promoteLevel;//有两个值，0、1。0-OrderLevel-订单级别，1-ProductLevel-产品级别
    private boolean isAutomatic;//是否自动
    private double DiscountAmount;//折扣金额
    
    //赠送的SKU
    private ArrayList<OrderProductModel> giftSkus;
    
    //距离过期的秒数
    private int EndTotalSeconds;
    
	public int getEndTotalSeconds() {
		return EndTotalSeconds;
	}

	public void setEndTotalSeconds(int endTotalSeconds) {
		EndTotalSeconds = endTotalSeconds;
	}

	public int getPromoteLevel() {
		return promoteLevel;
	}

	public void setPromoteLevel(int promoteLevel) {
		this.promoteLevel = promoteLevel;
	}

	public boolean isAutomatic() {
		return isAutomatic;
	}

	public void setAutomatic(boolean isAutomatic) {
		this.isAutomatic = isAutomatic;
	}

	public double getDiscountAmount() {
		return DiscountAmount;
	}

	public void setDiscountAmount(double discountAmount) {
		DiscountAmount = discountAmount;
	}

	public ArrayList<OrderProductModel> getGiftSkus() {
		return giftSkus;
	}

	public void setGiftSkus(ArrayList<OrderProductModel> giftSkus) {
		this.giftSkus = giftSkus;
	}

	public static Parcelable.Creator<CouponModel> getCreator() {
		return CREATOR;
	}

	public String getCouponCode() {
		return CouponCode;
	}

	public void setCouponCode(String couponCode) {
		CouponCode = couponCode;
	}

	public String getName() {
		return Name;
	}

	public void setName(String name) {
		Name = name;
	}

	public String getBeginDate() {
		return BeginDate;
	}

	public void setBeginDate(String beginDate) {
		BeginDate = beginDate;
	}

	public String getEndDate() {
		return EndDate;
	}

	public void setEndDate(String endDate) {
		EndDate = endDate;
	}

	public String getConditionDes() {
		return ConditionDes;
	}

	public void setConditionDes(String conditionDes) {
		ConditionDes = conditionDes;
	}

	public String getActionDes() {
		return ActionDes;
	}

	public void setActionDes(String actionDes) {
		ActionDes = actionDes;
	}

	public String getDescription() {
		return Description;
	}

	public void setDescription(String description) {
		Description = description;
	}

	public String getStatus() {
		return Status;
	}

	public void setStatus(String status) {
		Status = status;
	}

	public String getOrderNumber() {
		return OrderNumber;
	}

	public void setOrderNumber(String orderNumber) {
		OrderNumber = orderNumber;
	}

	public String getImgUrl() {
		return ImgUrl;
	}

	public void setImgUrl(String imgUrl) {
		ImgUrl = imgUrl;
	}
	//将json对象转换为Coupon对象
	public static CouponModel jsonObject2Coupon(JSONObject jsonObject){
		CouponModel  coupon = new CouponModel();
		if(jsonObject!=null){
			coupon.setActionDes(jsonObject.optString(ServerConstant.CouponField.Coupon_ActionDes));
			coupon.setBeginDate(jsonObject.optString(ServerConstant.CouponField.Coupon_BeginDate));
			coupon.setConditionDes(jsonObject.optString(ServerConstant.CouponField.Coupon_ConditionDes));
			coupon.setCouponCode(jsonObject.optString(ServerConstant.CouponField.Coupon_CouponCode));
			coupon.setDescription(jsonObject.optString(ServerConstant.CouponField.Coupon_Description));
			coupon.setEndDate(jsonObject.optString(ServerConstant.CouponField.Coupon_EndDate));
			coupon.setImgUrl(jsonObject.optString(ServerConstant.CouponField.Coupon_ImgUrl));
			coupon.setName(jsonObject.optString(ServerConstant.CouponField.Coupon_Name));
			coupon.setOrderNumber(jsonObject.optString(ServerConstant.CouponField.Coupon_OrderNumber));
			coupon.setStatus(jsonObject.optString(ServerConstant.CouponField.Coupon_Status));
			coupon.setPromoteLevel(jsonObject.optInt(ServerConstant.CouponField.Coupon_PromoteLevel));
			coupon.setAutomatic(jsonObject.optBoolean(ServerConstant.CouponField.Coupon_IsAutomatic));
			coupon.setDiscountAmount(jsonObject.optDouble(ServerConstant.CouponField.Coupon_DiscountAmount));
			coupon.setEndTotalSeconds(jsonObject.optInt("EndTotalSeconds"));
			JSONArray array = jsonObject.optJSONArray(ServerConstant.CouponField.Coupon_GiftSkus);
			if(array!=null && array.length()>0){
				ArrayList<OrderProductModel> list = new ArrayList<OrderProductModel>();
				for(int i = 0;i<array.length();i++){
					OrderProductModel model = new OrderProductModel();
					JSONObject object = (JSONObject) array.opt(i);
					model.setSku(object.optInt("SKU"));
					model.setQuantity(object.optInt("Qty"));
//					model.setWebUrl(object.optString("WebUrl"));
//					model.setShortHeadLine1(object.optString("ShortHeadLine"));
//					model.setBigPictureURL(object.optString("BigPictureURL"));
//					model.setMediumPictureURL(object.optString("MediumPictureURL"));
//					model.setSmallPictureURL(object.optString("SmallPictureURL"));
					
					list.add(model);
				}
				coupon.setGiftSkus(list);
			}
		}
		return coupon;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	public static final Parcelable.Creator<CouponModel> CREATOR = new Creator<CouponModel>() {
		
		@Override
		public CouponModel[] newArray(int size) {
			// TODO Auto-generated method stub
			return new CouponModel[size];
		}
		
		@Override
		public CouponModel createFromParcel(Parcel source) {
			// TODO Auto-generated method stub
			return new CouponModel(source);
		}
	};
	boolean[] automatics = new boolean[]{isAutomatic};
	@SuppressWarnings("unchecked")
	public CouponModel(Parcel source) {
		CouponCode = source.readString();
		Name = source.readString();
		BeginDate = source.readString();
		EndDate = source.readString();
		ConditionDes = source.readString();
		ActionDes = source.readString();
		Description = source.readString();
		Status = source.readString();
		OrderNumber = source.readString();
		ImgUrl = source.readString();
		promoteLevel = source.readInt();
		isAutomatic = (source.readInt() == 1?true : false);
		DiscountAmount = source.readDouble();
		giftSkus = source.readArrayList(OrderProductModel.class.getClassLoader());
		EndTotalSeconds = source.readInt();
	}
	public CouponModel(){}
	
	@Override
	public void writeToParcel(Parcel parcel, int flags) {
		parcel.writeString(CouponCode);
		parcel.writeString(Name);
		parcel.writeString(BeginDate);
		parcel.writeString(EndDate);
		parcel.writeString(ConditionDes);
		parcel.writeString(ActionDes);
		parcel.writeString(Description);
		parcel.writeString(Status);
		parcel.writeString(OrderNumber);
		parcel.writeString(ImgUrl);
		parcel.writeInt(promoteLevel);
		parcel.writeInt(isAutomatic?1:0);
		parcel.writeDouble(DiscountAmount);
		parcel.writeList(giftSkus);
		parcel.writeInt(EndTotalSeconds);
	}
}
